package com.cooksys.film_rental.bo.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cooksys.film_rental.bo.CustomerBo;
import com.cooksys.film_rental.dao.CustomerDao;
import com.cooksys.film_rental.model.Address;
import com.cooksys.film_rental.model.Customer;
import com.cooksys.film_rental.model.Film;
import com.cooksys.film_rental.model.Payment;
import com.cooksys.film_rental.model.Rental;
import com.cooksys.film_rental.model.Store;
import com.cooksys.film_rental.model.User;

@Service("CustomerBo")
public class CustomerBoImpl implements CustomerBo {

	@Autowired(required = true)
	CustomerDao customerDao;

	@Override
	public Customer save(Customer customer) {
		return this.customerDao.save(customer);

	}

	@Override
	public void update(Customer customer) {
		this.customerDao.update(customer);

	}

	@Override
	public void delete(Customer customer) {
		this.customerDao.delete(customer);

	}

	@Override
	public Customer getCustomerByCustomerId(Short customerId) {
		return this.customerDao.getCustomerByCustomerId(customerId);
	}

	@Override
	public Customer getCustomerByCustomerId(String customerId) {
		try {

			Short customerIdAsShort = Short.valueOf(customerId);
			return this.getCustomerByCustomerId(customerIdAsShort);

		} catch (NumberFormatException e) {

			System.err
					.println("Could not parse Short value for customerId from string: "
							+ customerId);
			e.printStackTrace();
			System.err.println("Returning null value...");
			return null;

		}

	}

	@Override
	public List<Customer> searchCustomersByAddress(Address address) {
		return this.customerDao.searchCustomersByAddress(address);
	}

	@Override
	public List<Customer> searchCustomersByStore(Store store) {
		return this.customerDao.searchCustomersByStore(store);
	}

	@Override
	public List<Customer> searchCustomersByPartialName(String partialName) {
		return this.customerDao.searchCustomersByPartialName(partialName);
	}

	@Override
	public Customer getCustomerByUser(User user) {
		return this.customerDao.getCustomerByUser(user);
	}

	@Override
	public Customer getCustomerByRental(Rental rental) {
		return this.customerDao.getCustomerByRental(rental);
	}

	@Override
	public Customer getCustomerByPayment(Payment payment) {
		return this.customerDao.getCustomerByPayment(payment);
	}

	@Override
	public void saveOrUpdate(Customer customer) {
		this.customerDao.saveOrUpdate(customer);

	}

	@Override
	public Customer matchCustomerToDatabase(Customer customer, Address address,
			Store store) {
		Customer result = null;

		if (customer.getCustomerId() != null) {

			result = this.getCustomerByCustomerId(customer.getCustomerId());

		}

		if (result == null) {

			result = this.customerDao.getCustomerByInfoAndAddressAndStore(
					customer.getFirstName(), customer.getLastName(),
					customer.getEmail(), address, store);

		}

		return result;
	}

	@Override
	public List<Film> getFilmsInCart(Customer customer) {
		return this.customerDao.getFilmsInCart(customer);
	}

	@Override
	public List<Film> getRentedFilms(Customer customer) {
		return this.customerDao.getRentedFilms(customer);
	}

	@Override
	public List<Rental> getRentalsInCart(Customer customer) {
		return this.customerDao.getRentalsInCart(customer);
	}

	@Override
	public List<Rental> getPaidRentals(Customer customer) {
		return this.customerDao.getPaidRentals(customer);
	}

}
